<?php
/**
 * Created by PhpStorm.
 * User: gyccvvh
 * Date: 2018/1/6
 * Time: 15:51
 */

namespace app\home\controller;


use Endroid\QrCode\QrCode;
use think\Cache;
use think\Controller;

class Scan extends Controller
{
    public $ret;
    public function __construct(){
        parent::__construct();
        $this->request = Request::instance();
        $this->now_uri = $this->request->module() . '/' . $this->request->controller() . '/' . $this->request->action();
        $this->now_uri = strtolower($this->now_uri);
        $this->now_url = $this->request->path();
        $this->ret = Ret::i();
    }
    public function index(){
        $seed = input('seed');
        $bar_code = uuid($seed);
        $this->assign("code",$bar_code);
        $content = $this->fetch();
        return $this->ret->setCode(0)->setMsg("请上传图片")->setData("bar_code",$bar_code)->setData("content",$content)->toJson();
    }

    public function path(){
        $token = input("token");
        $path = Cache::get($token,"");
        return $this->ret->setCode(0)->setData("pathes",$path)->toJson();
    }

    public function img(){
        $code = input('code');
        $text = "http://{$_SERVER['HTTP_HOST']}/home/scan/upload/token/{$code}.html";
        $qrCode = new QrCode();
        $qrCode->setText($text)
            ->setSize(500)
            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));
        header("Content-type: image/png");
        return $qrCode->writeString(); //输入到浏览器
    }

    public function upload(){
        $token = input('token');
        $files = Cache::get($token,'');
        if(!empty($files)) {
            $file_arr = explode(";", $files);
            $this->assign("files", $file_arr);
        }else{
            $this->assign("files", []);
        }
        $this->assign("filepath",$files);
        $this->assign("token",$token);
        return $this->fetch();
    }

    public function upload_path(){
        $no = input('no');
        $path = input('path');
        $res = Cache::set($no,$path,3600);
        if($res){
            return $this->ret->setCode(0)->setMsg("上传成功")->toJson();
        }else{
            return $this->ret->setCode(1)->setMsg("上传失败")->toJson();
        }
    }

}